#include "randomrunner.h"
#include "ui_dialog.h"
#include <cstdlib>

RandomRunner::RandomRunner(int people, int runtimer_interval, double stoptimer_min_interval,
               double stoptimer_max_interval, QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    this->people = people;
    this->runtimer_interval = runtimer_interval;
    this->stoptimer_min_interval = stoptimer_min_interval;
    this->stoptimer_max_interval = stoptimer_max_interval;
    this->result = -1;

    ui->setupUi(this);
    connect(&runtimer, SIGNAL(timeout()), this, SLOT(run()));
    connect(&stoptimer, SIGNAL(timeout()), this, SLOT(stop()));
    srand((unsigned int)time(nullptr));
    runtimer.setInterval(runtimer_interval);
}

void RandomRunner::run()
{
    if (result < 0){
        result = 0;
    }

    result = (result + 1) % people;
    ui->label->setText(QString::number(result));
}

void RandomRunner::stop()
{
    runtimer.stop();
    stoptimer.stop();
}


RandomRunner::~RandomRunner()
{
    delete ui;
}


void RandomRunner::on_pushButton_clicked()
{
    int stoptimer_interval = (int)(1000 * (stoptimer_max_interval - stoptimer_min_interval));
    int time = rand() % stoptimer_interval + (int)(1000 * stoptimer_min_interval);
    stoptimer.setInterval(time);
    stoptimer.start();
    runtimer.start();
}
